package com.codebuffer.nowcoder;

public class GetNext {

    public TreeLinkNode GetNext(TreeLinkNode pNode) {

        TreeLinkNode right = pNode.right;
        if(null!=right){
            TreeLinkNode left = right;
            while (null != left.left){
                left = left.left;
            }
            return left;
        }else {
            TreeLinkNode next = pNode.next;
            while (null!=next && next.left != pNode){
                pNode = next;
                next = next.next;
            }
            return next;
        }



    }
}


class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}